package gui.customizer.panes.dhcp;

import java.awt.event.ActionListener;
import java.util.HashMap;

import javax.swing.BorderFactory;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;

import net.miginfocom.swing.MigLayout;
import javax.swing.JCheckBox;
import javax.swing.JButton;

import core.models.DhcpSubnet;

public class SubnetEditor extends JPanel {

	/**
	 * 
	 */
	private static final long serialVersionUID = 6564692748569249713L;
	private JTextField txtNetworkAddress;
	private JTextField txtNetmask;
	private JPanel rangePanel;
	private JLabel lblBegin;
	private JLabel lblEnd;
	private JTextField txtRangeEnd;
	private JTextField txtRangeBegin;
	private JLabel lblDefaultLeaseTime;
	private JLabel lblMaxLeaseTime;
	private JTextField txtDefaultLeaseTime;
	private JTextField txtMaxLeaseTime;
	private JCheckBox chckbxIpForwarding;
	private JPanel netOptionsPanel;
	private JLabel lblDefaultGateway;
	private JTextField txtDefaultGateway;
	private JLabel lblDefaultSubnetMask;
	private JTextField txtDefaultSubnetMask;
	private JLabel lblDefaultBraodcastAddress;
	private JTextField txtDefaultBroadcastAddress;
	private JLabel lblDomainNameServers;
	private JTextField txtDomainNameServers;
	private JLabel lblDefaultNtpServers;
	private JTextField txtDefaultNtpServers;
	private JLabel lblDefaultNetbiosServers;
	private JTextField txtDefaultNetbiosServers;
	private JButton btnApplyChanges;

	/**
	 * Create the panel.
	 */
	public SubnetEditor() {
		setLayout(new MigLayout("", "[150px]20[150px][]", "[][]15[]15[][]15[]15[]10[]"));
		
		// Required - Network, Netmask
		JLabel lblNetworkAddress = new JLabel("Network Address");
		add(lblNetworkAddress, "cell 0 0");
		
		JLabel lblNetmask = new JLabel("Netmask");
		add(lblNetmask, "cell 1 0");
		
		txtNetworkAddress = new JTextField();
		add(txtNetworkAddress, "cell 0 1,growx");
		txtNetworkAddress.setColumns(10);
		
		txtNetmask = new JTextField();
		add(txtNetmask, "cell 1 1,growx");
		txtNetmask.setColumns(10);
		
		
		// Range Panel
		rangePanel = new JPanel();
		add(rangePanel, "cell 0 2 3 1,grow");
		rangePanel.setLayout(new MigLayout("", "[grow]15[grow]", "[][]"));
		rangePanel.setBorder(BorderFactory.createTitledBorder("Range"));
		
		lblBegin = new JLabel("Begin");
		rangePanel.add(lblBegin, "cell 0 0");
		
		lblEnd = new JLabel("End");
		rangePanel.add(lblEnd, "cell 1 0");
		
		txtRangeBegin = new JTextField();
		rangePanel.add(txtRangeBegin, "cell 0 1,growx");
		txtRangeBegin.setColumns(10);
		
		txtRangeEnd = new JTextField();
		rangePanel.add(txtRangeEnd, "cell 1 1,growx");
		txtRangeEnd.setColumns(10);
		
		// Leases  and ip forwarding
		lblDefaultLeaseTime = new JLabel("Default Lease Time");
		add(lblDefaultLeaseTime, "cell 0 3");
		
		lblMaxLeaseTime = new JLabel("Max Lease Time");
		add(lblMaxLeaseTime, "cell 1 3");
		
		txtDefaultLeaseTime = new JTextField();
		add(txtDefaultLeaseTime, "cell 0 4,growx");
		txtDefaultLeaseTime.setColumns(10);
		
		txtMaxLeaseTime = new JTextField();
		add(txtMaxLeaseTime, "cell 1 4,growx");
		txtMaxLeaseTime.setColumns(10);
		
		chckbxIpForwarding = new JCheckBox("IP Forwarding");
		chckbxIpForwarding.setSelected(false);
		add(chckbxIpForwarding, "cell 0 5");
		
		// Net options panel
		netOptionsPanel = new JPanel();
		add(netOptionsPanel, "cell 0 6 3 1,grow");
		netOptionsPanel.setLayout(new MigLayout("", "[grow]15[grow]15[grow]", "[][]15[][]"));
		netOptionsPanel.setBorder(BorderFactory.createTitledBorder("Network Options"));
		
		lblDefaultGateway = new JLabel("Default Gateway");
		netOptionsPanel.add(lblDefaultGateway, "cell 0 0");
		
		lblDefaultSubnetMask = new JLabel("Default Subnet Mask");
		netOptionsPanel.add(lblDefaultSubnetMask, "cell 1 0");
		
		lblDefaultBraodcastAddress = new JLabel("Default Braodcast Address");
		netOptionsPanel.add(lblDefaultBraodcastAddress, "cell 2 0");
		
		txtDefaultGateway = new JTextField();
		netOptionsPanel.add(txtDefaultGateway, "cell 0 1,growx");
		txtDefaultGateway.setColumns(10);
		
		txtDefaultSubnetMask = new JTextField();
		netOptionsPanel.add(txtDefaultSubnetMask, "cell 1 1,growx");
		txtDefaultSubnetMask.setColumns(10);
		
		txtDefaultBroadcastAddress = new JTextField();
		netOptionsPanel.add(txtDefaultBroadcastAddress, "cell 2 1,growx");
		txtDefaultBroadcastAddress.setColumns(10);
		
		lblDomainNameServers = new JLabel("Domain Name Servers");
		netOptionsPanel.add(lblDomainNameServers, "cell 0 2");
		
		lblDefaultNtpServers = new JLabel("Default NTP Servers");
		netOptionsPanel.add(lblDefaultNtpServers, "cell 1 2");
		
		lblDefaultNetbiosServers = new JLabel("Default Netbios Name Servers");
		netOptionsPanel.add(lblDefaultNetbiosServers, "cell 2 2");
		
		txtDomainNameServers = new JTextField();
		netOptionsPanel.add(txtDomainNameServers, "cell 0 3,growx");
		txtDomainNameServers.setColumns(10);
		
		txtDefaultNtpServers = new JTextField();
		netOptionsPanel.add(txtDefaultNtpServers, "cell 1 3,growx");
		txtDefaultNtpServers.setColumns(10);
		
		txtDefaultNetbiosServers = new JTextField();
		netOptionsPanel.add(txtDefaultNetbiosServers, "cell 2 3,growx");
		txtDefaultNetbiosServers.setColumns(10);
		
		btnApplyChanges = new JButton("Apply Changes");
		btnApplyChanges.setVisible(false);
		add(btnApplyChanges, "cell 0 7");
	}

	public void setSubnet(DhcpSubnet subnet) {
		HashMap<String, String> options = subnet.getOptions();
		
		// Network
		txtNetworkAddress.setText(subnet.getNetwork());
		
		// Netmask
		txtNetmask.setText(subnet.getNetmask());
		
		// Range
		if (options.containsKey("range")) {
			String[] range = options.get("range").split(" ");
			txtRangeBegin.setText(range[0]);
			txtRangeEnd.setText(range[1]);
		} else {
			txtRangeBegin.setText("");
			txtRangeEnd.setText("");
		}
		
		
		// Lease Times
		setTextField(options, txtDefaultLeaseTime, "default-lease-time");
		setTextField(options, txtMaxLeaseTime, "max-lease-time");
		
		// IP Forwarding
		if (options.containsKey("option ip-forwarding")) {
			String setting = options.get("option ip-forwarding");
			if (setting.equalsIgnoreCase("on")) {
				chckbxIpForwarding.setSelected(true);
			} else if (setting.equalsIgnoreCase("off")) {
				chckbxIpForwarding.setSelected(false);
			} else {
				chckbxIpForwarding.setSelected(false);
			}
		}
		
		// Gateway
		setTextField(options, txtDefaultGateway, "option routers");
		
		// Subnet Mask
		setTextField(options, txtDefaultSubnetMask, "option subnet-mask");
		
		// Broadcast
		setTextField(options, txtDefaultBroadcastAddress, "option broadcast-address");
		
		// Domain Name Servers
		setTextField(options, txtDomainNameServers, "option domain-name-servers");
		
		// Default NTP Servers
		setTextField(options, txtDefaultNtpServers, "option ntp-servers");
		
		// Default Netbios Names Server
		setTextField(options, txtDefaultNetbiosServers, "option netbios-name-servers");
		
		btnApplyChanges.setVisible(true);
	}
	
	public void hideApplyButton() {
		btnApplyChanges.setVisible(false);
	}

	public DhcpSubnet getSubnet() {
		// Network and Netmask
		DhcpSubnet subnet = new DhcpSubnet(txtNetworkAddress.getText(), txtNetmask.getText());
		HashMap<String, String> options = subnet.getOptions();
		
		// Range
		if (!txtRangeBegin.getText().equals("") || !txtRangeEnd.getText().equals("")) {
			options.put("range", txtRangeBegin.getText() + " " + txtRangeEnd.getText());
		}
		
		// Lease Times
		addOptionFromTextField(options, txtDefaultLeaseTime, "default-lease-time");
		addOptionFromTextField(options, txtMaxLeaseTime, "max-lease-time");
		
		// IP Forwarding
		if (chckbxIpForwarding.isSelected()) {
			options.put("option ip-forwarding", "on");
		} else {
			options.put("option ip-forwarding", "off");
		}

		// Gateway
		addOptionFromTextField(options, txtDefaultGateway, "option routers");

		// Subnet Mask
		addOptionFromTextField(options, txtDefaultSubnetMask,
				"option subnet-mask");

		// Broadcast
		addOptionFromTextField(options, txtDefaultBroadcastAddress,
				"option broadcast-address");

		// Domain Name Servers
		addOptionFromTextField(options, txtDomainNameServers,
				"option domain-name-servers");

		// Default NTP Servers
		addOptionFromTextField(options, txtDefaultNtpServers, "option ntp-servers");

		// Default Netbios Names Server
		addOptionFromTextField(options, txtDefaultNetbiosServers,
				"option netbios-name-servers");
		
		return subnet;
	}
	
	public void addListenerToApplyButton(ActionListener l) {
		btnApplyChanges.addActionListener(l);
	}
	
	private void setTextField(HashMap<String, String> options, JTextField txtField, String key) {
		if (options.containsKey(key)) {
			txtField.setText(options.get(key));
		} else {
			txtField.setText("");
		}
	}
	
	private void addOptionFromTextField(HashMap<String, String> options, JTextField txtField, String key) {
		if (!txtField.getText().equals("")) {
			options.put(key, txtField.getText());
		}
	}
}
